{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 访问Github API, ref: https://docs.github.com/cn/rest/search#search-repositories\n",
    "# githubUrl = 'https://api.github.com/search/repositories?q=react+language:javascript&sort=stars&order=desc&per_page=10'\n",
    "# print(\"查询Github中star最多的react项目\")\n",
    "# response = requests.get(githubUrl)\n",
    "# print(\"响应状态：\", response.status_code)\n",
    "# data = response.json()\n",
    "\n",
    "# 读取保存的数据\n",
    "dataFile=open(\"response_data.json\",\"r\")\n",
    "data=json.load(dataFile)\n",
    "dataFile.close()\n",
    "# 取出数据\n",
    "items = data['items']\n",
    "\n",
    "# 将查询结果写入文件\n",
    "# dataFile=open(\"response_data.json\",\"w\")\n",
    "# dataFile.write(json.dumps(data))\n",
    "# dataFile.close()\n",
    "# data=json.load(open(\"response_data.json\",\"r\"))\n",
    "\n",
    "# 横坐标\n",
    "names = [item['full_name'] for item in items]\n",
    "# 纵坐标\n",
    "stars = [item['stargazers_count'] for item in items]\n",
    "# 柱形图 ref: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html?highlight=bar#matplotlib.pyplot.bar\n",
    "# 创建带有一个坐标系的图像\n",
    "fig, ax = plt.subplots()\n",
    "ax.bar(names, stars)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建包含一个坐标系的图像，图像大小为（10 in，5 in）\n",
    "fig, ax = plt.subplots(figsize=(10,5))\n",
    "# width为柱的宽度\n",
    "ax.bar(names, stars, width=0.5)\n",
    "# 图表标题\n",
    "ax.set_title(\"Most Stars Project of React\")\n",
    "# 纵轴标签\n",
    "ax.set_ylabel(\"Star\")\n",
    "# 设置横柚刻度样式，rotation设置为'垂直方向'\n",
    "ax.set_xticks(names, names, rotation='vertical')\n",
    "# 设置纵轴刻度\n",
    "ax.set_yticks([0,50000,100000,150000,200000],('0','50k','100k','150k','200k'))\n",
    "# 在柱上显示star数量\n",
    "for a, b in zip(names, stars):\n",
    "    # text(x,y,str)可在图中(x,y)位置添加文本str\n",
    "    ax.text(a, b, b, ha='center',va='bottom', fontsize=10)  # fontsize表示柱坐标上显示字体的大小\n",
    "\n",
    "# ax.margins()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.2 ('venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "1577a05ddfca352f4d657d7bbde71d44e58f3858e3f81e48a10db2a97469c5a1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
